package com.mce.nebula.util;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 验证工具类
 * 对应Python的验证功能
 */
public class ValidationUtils {
    
    // 敏感词列表
    private static final List<String> SENSITIVE_WORDS = Arrays.asList(
        "root", "goodrain", "builder", "app", "tenant", "tenants", 
        "service", "services","administrator", "system"
    );
    
    // 标准用户名正则表达式
    private static final Pattern STANDARD_USERNAME_PATTERN = Pattern.compile("^[a-z0-9][a-z0-9_\\-]+[a-z0-9]$");
    
    // 扩展用户名正则表达式（用于k8s命名空间）
    private static final Pattern EXTENDED_USERNAME_PATTERN = Pattern.compile("^[a-z0-9][a-z0-9\\-]+[a-z0-9]$");
    
    /**
     * 检查用户名是否为敏感词
     */
    public static boolean isSensitiveWord(String username) {
        if (username == null) {
            return false;
        }
        return SENSITIVE_WORDS.contains(username.toLowerCase());
    }
    
    /**
     * 检查用户名是否符合标准格式
     */
    public static boolean isStandardUsername(String username) {
        if (username == null) {
            return false;
        }
        return STANDARD_USERNAME_PATTERN.matcher(username).matches();
    }
    
    /**
     * 检查用户名是否符合扩展格式（用于k8s命名空间）
     */
    public static boolean isExtendedUsername(String username) {
        if (username == null) {
            return false;
        }
        return EXTENDED_USERNAME_PATTERN.matcher(username).matches();
    }
    
    /**
     * 规范化用户名（转换为符合k8s命名空间规范的名称）
     * 对应Python的normalize_name_for_k8s_namespace函数
     */
    public static String normalizeNameForK8sNamespace(String name) {
        if (name == null || name.trim().isEmpty()) {
            return name;
        }
        
        // 转换为小写
        String normalized = name.toLowerCase();
        
        // 替换下划线为连字符
        normalized = normalized.replace("_", "-");
        
        // 移除不符合k8s命名空间规范的字符
        normalized = normalized.replaceAll("[^a-z0-9\\-]", "");
        
        // 确保以字母或数字开头和结尾
        if (!normalized.isEmpty()) {
            if (!Character.isLetterOrDigit(normalized.charAt(0))) {
                normalized = "u" + normalized;
            }
            if (!Character.isLetterOrDigit(normalized.charAt(normalized.length() - 1))) {
                normalized = normalized + "0";
            }
        }
        
        return normalized;
    }
    
    /**
     * 验证密码强度
     */
    public static boolean isStrongPassword(String password) {
        if (password == null || password.length() < 8) {
            return false;
        }
        
        // 检查是否包含至少一个字母和一个数字
        boolean hasLetter = password.chars().anyMatch(Character::isLetter);
        boolean hasDigit = password.chars().anyMatch(Character::isDigit);
        
        return hasLetter && hasDigit;
    }
    
    /**
     * 验证邮箱格式
     */
    public static boolean isValidEmail(String email) {
        if (email == null || email.trim().isEmpty()) {
            return false;
        }
        
        String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
        return Pattern.matches(emailPattern, email);
    }
    
    /**
     * 验证手机号格式
     */
    public static boolean isValidPhone(String phone) {
        if (phone == null || phone.trim().isEmpty()) {
            return false;
        }
        
        // 简单的手机号验证，支持11位数字
        String phonePattern = "^1[3-9]\\d{9}$";
        return Pattern.matches(phonePattern, phone);
    }
}
